/*
 *Author: 王鹏勇
 *Copyright (c) 2012 88ddqq@163.com
 *http://www.itylw.com/
 *date: 2012-9-24
 *desc:此游戏灵感来自于网络中的绵羊快跑
 */
//游戏关
var gameRunning = false;
//画布宽,高
var mapWidth = 800;
var mapHeight = 600;

var imgWpLeft=imgWpRight = "http://www.itylw.com/img/game/lwp.gif";

var imgWolfLeft = "http://www.itylw.com/img/game/lwolf.gif";
var imgWolfRight = "http://www.itylw.com/img/game/rwolf.gif";

//-------------------------王萍模型相关-------------------------------
//王萍模型的高度:40,宽度:27
var wpWidth = 40;
var wpHeight = 27;

//起始横坐标wpXcurrnet:380,起始枞坐标默认为 wpYcurrnet: 286
var wpXcurrnet = parseInt((mapWidth - wpWidth) / 2);
var wpYcurrnet = parseInt((mapHeight - wpHeight) / 2);

//移动速度
var wpXspeed = 0;
var wpYspeed = 0;
//摩擦力,跑得快慢问题
var wpFriction = 0.85;
//--------------------------狼模型相关-----------------------------
//狼模型的高度:40,宽度:27
var wolfWidth = 40;
var wolfHeight = 27;

//狼群最大数量
var maxWolfs = 200;
//狼出现时间间隔（默认1000毫秒）
var appearTime = 1000;

//移动速度
var wolfXspeed = 0;
var wolfYspeed = 0;

//摩擦力
var wolfFriction = 0.80;
/* 狼群 */
var wolfs = new Array();

//-------------------------方向键按键相关------------------------------
//方向键按键设置
var keyUp = 87;//w
var moveUp = false;
var keyDown = 83;//s
var moveDown = false;
var keyLeft = 65;//a
var moveLeft = false;
var keyRight = 68;//d
var moveRight = false;

//积分
var score = 0;
//画布上下文
var context;
var wolfTimeout;
/**
 * 程序开始,每30ms执行一次
 */
function start() {
	var map = document.getElementById('map');
	context = map.getContext('2d');
	//将画布上清空
	context.clearRect(0, 0, mapWidth, mapHeight);
	drawSheep();
	drawWolfs();
}

/**
 * 绘制王萍
 */
function drawSheep() {
	var wpImg = new Image();
	wpImg.src = imgWpLeft;
	if (moveLeft) {
		wpXspeed--;
	}
	if (moveRight) {
		wpXspeed++;
		wpImg.src = imgWpRight;//转头向右跑
	}
	if (moveUp) {
		wpYspeed--;
	}
	if (moveDown) {
		wpYspeed++;
	}
	context.drawImage(wpImg, wpXcurrnet, wpYcurrnet);
	context.fill();
	/* 移动距离计算 */

	if (wpXcurrnet >= 0 && wpXcurrnet <= (mapWidth - wpWidth)) {
		wpXcurrnet += wpXspeed;
	} else if (wpXcurrnet < 0) {
		wpXcurrnet += Math.abs(wpXspeed);
	} else if (wpXcurrnet > (mapWidth - wpWidth)) {
		wpXcurrnet -= Math.abs(wpXspeed);
	}

	if (wpYcurrnet >= 0 && wpYcurrnet <= (mapHeight - wpHeight)) {
		wpYcurrnet += wpYspeed;
	} else if (wpYcurrnet < 0) {
		wpYcurrnet += Math.abs(wpYspeed);
	} else if (wpYcurrnet > (mapHeight - wpHeight)) {
		wpYcurrnet -= Math.abs(wpYspeed);
	}
	wpXspeed *= wpFriction;
	wpYspeed *= wpFriction;
}
/**
 * 在狼群中增加一个狼
 */
function createWolfs() {
	if(wolfs.length >= maxWolfs-1)
		clearInterval(wolfTimeout);
	/* 0123 上下左右 */
	direction = parseInt(Math.random() * 4);
	/* 狼随机出现的x y 坐标 */
	wx = parseInt(Math.random() * (mapWidth - wolfWidth));
	wy = parseInt(Math.random() * (mapHeight - wolfHeight));
	
	var wolfObj = new Object();

	switch (direction) {
		case 0://狼从上轴出现
			wolfObj.x = wx;
			wolfObj.y = 0;
			break;
		case 1://狼从下轴出现
			wolfObj.x = wx;
			wolfObj.y = mapHeight - wolfHeight;
			break;
		case 2://狼从左轴出现
			wolfObj.x = 0;
			wolfObj.y = wy;
			break;
		case 3://狼从右轴出现
			wolfObj.x = mapWidth - wolfWidth;
			wolfObj.y = wy;
			break;
	}
	//将产生的狼放入狼群
	wolfs.push(wolfObj);
}

/**
 * 显示狼，并判断王萍有没有碰到王萍
 */
function drawWolfs() {
	var wolfCounter = document.getElementById("wolfCounter");
	wolfCounter.innerHTML = wolfs.length;
	for (i = 0; i < wolfs.length; i++) {
		var wolf = new Image();
		wolf.src = imgWolfLeft;
		
		if (wolfs[i].x > wpXcurrnet) {
			wolfXspeed--;
		} else {
			wolfXspeed++;
			wolf.src = imgWolfRight;
		}

		if (wolfs[i].y > wpYcurrnet) {
			wolfYspeed--;
		} else {
			wolfYspeed++;
		}

		context.drawImage(wolf, wolfs[i].x, wolfs[i].y);

		if (Math.abs(wolfs[i].x - wpXcurrnet) <= parseInt(wpWidth) && Math.abs(wolfs[i].y - wpYcurrnet) <= parseInt(wpHeight)) {
			catchWp();
		} else {
			wolfs[i].x += wolfXspeed;
			wolfs[i].y += wolfYspeed;
		}
		//这里狼不能比羊跑得快,所以*了一下
		wolfXspeed *= wpFriction * wolfFriction;
		wolfYspeed *= wpFriction * wolfFriction;
	}
}

/**
 * 游戏开关
 */
function ToggleGameplay() {
	if (gameRunning) {
		begin = setInterval(start, 30);
		createWolfs();
		wolfTimeout = setInterval(createWolfs,appearTime);
	} else {
		clearInterval(begin);
	}
}
/**
 * 被捕
 */
function catchWp() {
	$('#result').text('你被捕了！最大狼数：' + $('#wolfCounter').text());
	$('#start').text('再试一次');
	$('#option').show();
	clearInterval(begin);
	clearInterval(wolfTimeout);
	wolfs = Array();
	wpXcurrnet = parseInt((mapWidth - wpWidth) / 2);
	wpYcurrnet = parseInt((mapHeight - wpHeight) / 2);
}

$(document).ready(function() {
	$('#option').show();

	// 点击这里时游戏开始
	$('#start').click(function() {
		$('#option').hide();
		gameRunning = true;
		ToggleGameplay();
	});

	$('#menu li').hover(function() {
		$(this).css({
			'background-color' : '#E8E8E8',
			'color' : '#FF0000',
			'font-size' : '36px',
			'font-style' : 'normal',
			'cursor' : 'pointer'
		});
	}, function() {
		$(this).css({
			'color' : '#FFFFFF',
			'background-color' : 'transparent',
			'font-size' : '32px',
			'font-style' : 'italic'
		});
	});

	// 点击帮助的时候
	var helpOn = 1;
	$('#helpMe').click(function() {
		if (helpOn == 1) {
			$('#help').fadeIn();
		} else {
			$('#help').fadeOut();
		}
		helpOn = (helpOn == 1) ? 0 : 1
	});

	/* 方向控制键监听 */
	$(document).keydown(function(event) {
		switch (event.keyCode) {
		case keyLeft || 37:
			moveLeft = true;
			break;
		case keyUp || 38:
			moveUp = true;
			break;
		case keyRight || 39:
			moveRight = true;
			break;
		case keyDown || 40:
			moveDown = true;
			break;
		}
	});

	$(document).keyup(function(event) {
		switch (event.keyCode) {
		case keyLeft || 37:
			moveLeft = false;
			break;
		case keyUp || 38:
			moveUp = false;
			break;
		case keyRight || 39:
			moveRight = false;
			break;
		case keyDown || 40:
			moveDown = false;
			break;
		}
	});
});